package com.example.lovedogs.mycenter;


import com.example.lovedogs.R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MenuMyPet extends Activity implements OnClickListener {
	private ImageView ivBack;
	private Button btn_add;
	ListView lv;
	Intent intent;
	ImageView flagImage;
	private LayoutInflater inflater;
	String[] names = new String[] { "bolivia", "brazil", "canada", "china",
			"japan" };
	int[] namesId = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher,
			R.drawable.ic_launcher, R.drawable.ic_launcher,
			R.drawable.ic_launcher };

	protected void onCreate(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		this.inflater = inflater;
		View rootView = inflater.inflate(R.layout.activity_menu_my_pet, container,
				false);// 关联布局文件
		init();
	}

	private void init() {
		ivBack = (ImageView) findViewById(R.id.iv_menu_mypet_back);
		ivBack.setOnClickListener(this);
		btn_add = (Button) findViewById(R.id.btn_addpet);
		btn_add.setOnClickListener(this);
		lv = (ListView) findViewById(R.id.listView1);
		lv.setAdapter(new MyAdapter());

	}

	@Override
	public void onClick(View v) {
		if (v == ivBack) {
			this.finish();
		} else if (v == btn_add) {// 添加宠物页面
			Toast.makeText(getApplicationContext(), "添加宠物", 0).show();
		}

	}

	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return names.length;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(final int position, View view, ViewGroup group) {
			LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(
					R.layout.item, null);
			flagImage = (ImageView) layout.findViewById(R.id.item_img);
			// flagImage.getDrawable(namesId[position]);
			flagImage.setImageResource(namesId[position]);
			final Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
					namesId[position]);
			layout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					intent = getIntent();
					intent.putExtra("bitmap", bitmap);
					intent.putExtra("name", names);
					intent.setClass(getApplicationContext(),
							SettingActivity.class);
					startActivity(intent);
				}
			});
			TextView flagName = (TextView) layout.findViewById(R.id.item_tv);
			flagName.setText(names[position]);
			return layout;
		}

	}

}
